/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.asu.cse360.client.util;

import edu.asu.cse360.client.action.CallForAssistanceAction;
import edu.asu.cse360.client.action.EnterEmployeeMode;
import edu.asu.cse360.client.ui.frame.AppFrame;
import edu.asu.cse360.client.ui.frame.OrderStatusPanel;
import edu.asu.cse360.client.ui.frame.PayPanel;
import edu.asu.cse360.client.ui.frame.ViewMenuPanel;
import edu.asu.cse360.model.ItemOrder;
import edu.asu.cse360.model.Order;
import edu.asu.cse360.model.OrderStatus;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

/**
 * This class displays the frame for the customer
 * @author nadimhoque
 */
public class CustomerConfiguration extends AppConfiguration{
    private PayPanel payPanel = PayPanel.instance(this);
    private OrderStatusPanel orderStatusPanel = new OrderStatusPanel();
    private ViewMenuPanel viewMenuPanel = ViewMenuPanel.instance(this);

    private static CustomerConfiguration instance;
    public static CustomerConfiguration instance(){
        if(instance == null){
            instance = new CustomerConfiguration();
        }
        return instance;
    }

    private CustomerConfiguration()
    {
        orderStatusPanel.addOrderStatusListener(new OrderStatusListener() {

            public void handleOrderStatusChange(ItemOrder order, OrderStatus status) {
                if(status == OrderStatus.CANCELLED)
                    payPanel.removeOrder(order);
                }
            });
    }

    @Override
    public void configure(AppFrame frame) {
        super.reset(frame);
        frame.showCard("customerCard");
        addMenuBar(frame);
        if(!hasConfiguredAppFrame){
            addPanels(frame);
            hasConfiguredAppFrame = true;
        }
        frame.getCustomerTabbedPane().setEnabledAt(0, true);
        frame.getCustomerTabbedPane().setEnabledAt(1, false);
        frame.getCustomerTabbedPane().setEnabledAt(2, false);
        viewMenuPanel.reset();
        payPanel.reset();
        frame.getCustomerTabbedPane().setSelectedComponent(frame.getCustomerMenuContainer());
    }

    @Override
    protected List<JMenu> getMenus(AppFrame frame) {
        List<JMenu> menus = new ArrayList<JMenu>();
       // menus.add(getSettingsMenu());
        menus.add(getCustomerHelp());
        menus.add(getCustomerAdminMenu());
        return menus;
    }

    protected JMenu getCustomerHelp(){
        JMenu menu = getHelpMenu();
        menu.add(new JMenuItem(new CallForAssistanceAction()));
        return menu;
    }

    protected JMenu getCustomerAdminMenu(){
        JMenu menu = new JMenu("Admin");
        menu.add(new JMenuItem(new EnterEmployeeMode()));
        return menu;
    }

    private void addPanels(AppFrame frame) {
        addPanel(frame.getCustomerMenuContainer(), viewMenuPanel);
        addPanel(frame.getCustomerOrderContainer(), orderStatusPanel);
        addPanel(frame.getCustomerPaymentContainer(), payPanel);
    }

    public void showPayPanel(Order order) {
        AppFrame appFrame = AppFrame.getAppFrame();
        JPanel payContainer = appFrame.getCustomerPaymentContainer();
        appFrame.getCustomerTabbedPane().setSelectedComponent(payContainer);
        payPanel.setOrder(order);
    }
    public void showMenuPanel(){
        AppFrame appFrame = AppFrame.getAppFrame();
        JPanel menuContainer = appFrame.getCustomerMenuContainer();
        appFrame.getCustomerTabbedPane().setSelectedComponent(menuContainer);
    }

    public void postPlaceOrder(Order order){
        AppFrame appFrame = AppFrame.getAppFrame();
        JPanel orderContainer = appFrame.getCustomerOrderContainer();
        appFrame.getCustomerTabbedPane().setSelectedComponent(orderContainer);
        orderStatusPanel.setOrder(order);
        payPanel.setOrder(order);
        appFrame.getCustomerTabbedPane().setEnabledAt(1, true);
        appFrame.getCustomerTabbedPane().setEnabledAt(2, true);
        appFrame.getCustomerTabbedPane().setEnabledAt(0, false);
    }


}
